#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   Bootloader_SID_Not_Supprot_test.py
@Time    :   2022/07/06 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   YangLiang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from numpy import byte, bytes_, int16
from pytest_assume.plugin import assume
 
import pytest
import time
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

SID_Req = [    ([0x14,0xFF,0xFF,0xFF],[0x7F,0x14,0x7F]),  
               ([0x19,0x02,0x0B],[0x7F,0x19,0x7F]),
               ([0x28,0x00,0x01],[0x7F,0x28,0x7F]),
               ([0x28,0x03,0x01],[0x7F,0x28,0x7F]),
               ([0x85,0x02],[0x7F,0x85,0x7F]),     
               ([0x85,0x01],[0x7F,0x85,0x7F]),
               ([0x31,0x02,0x12,0x7A],[0x7F,0x31,0x31]),
               ([0x31,0x82,0x12,0x7B],[0x7F,0x31,0x31])]


class Test_SID_Not_Supprot_Phy(object):
     @allure.severity('normal')
     @allure.story('ECU not support SID test')
     @allure.description('Test SID that not supported in bootloader')
     @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/6306e0482ce6d84a055b6e90/cases')
     @pytest.mark.parametrize('req, resp', SID_Req,ids=["req:{0}".format(binascii.b2a_hex(bytearray(req),' '), resp) for req, resp in SID_Req])
     def test_SID_Not_Supprot_Phy(self,action,Driver,req,resp):
          with allure.step('step1:Request:0x10,0x01, # Response:0x50,0x01,0x00,0x32,0x00,0xC8'):
               uds_rep,size = Driver['UDS'].req_and_res_can([0x10,0x01],2)
               if  uds_rep[0:6] != [0x50,0x01,0x00,0x32,0x00,0xC8]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
                logging.error(f'correct response:[0x50,0x01,0x00,0x32,0x00,0xC8], actual response:[{actuRespStr}]')
               # with assume: assert uds_rep[0:6] == [0x50,0x01,0x00,0x32,0x00,0xC8]
          with allure.step('step2:Request:0x10,0x03, # Response:0x50,0x03'):
               uds_rep,size = Driver['UDS'].req_and_res_can([0x10,0x03],2)
               if  uds_rep[0:6] != [0x50,0x03,0x00,0x32,0x00,0xC8]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
                logging.error(f'correct response:[0x50,0x03,0x00,0x32,0x00,0xC8], actual response:[{actuRespStr}]')
               with assume: assert uds_rep[0:6] == [0x50,0x03,0x00,0x32,0x00,0xC8]
               uds_rep,size = Driver['UDS'].req_and_res_can([0x22,0x12,0x60],3)
               uds_rep,size = Driver['UDS'].req_and_res_can([0x22,0x12,0x61],3)
     
          with allure.step('step3:Request:0x31,0x01,0x02,0x03, # Response:0x71,0x01,0x02,0x03'):
               uds_rep,size = Driver['UDS'].req_and_res_can([0x31,0x01,0x02,0x03],4)
               if  uds_rep[0:4] != [0x71,0x01,0x02,0x03]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
                logging.error(f'correct response:[0x71,0x01,0x02,0x03], actual response:[{actuRespStr}]')
               with assume: assert uds_rep[0:4] == [0x71,0x01,0x02,0x03]

          with allure.step('step4:Request:0x10,0x02, # Response:0x50,0x02'):
               uds_rep,size = Driver['UDS'].req_and_res_can([0x10,0x02],2)
               if  uds_rep[0:6] != [0x50,0x02,0x00,0x32,0x00,0xC8]:
                actuRespStr = binascii.b2a_hex(bytearray(uds_rep[:]), ' ') 
                logging.error(f'correct response:[0x50,0x02,0x00,0x32,0x00,0xC8], actual response:[{actuRespStr}]')
               with assume: assert uds_rep[0:6] == [0x50,0x02,0x00,0x32,0x00,0xC8]
               time.sleep(3)
          with allure.step('step5:Request:{0}, # Response:{1}'.format(binascii.b2a_hex(bytearray(req),' '),binascii.b2a_hex(bytearray(resp),' '))):
               uds_rep,size = Driver['UDS'].req_and_res_can(req,len(req))
               with assume: assert size == 3
               if size > 0:
                    if  uds_rep != resp:
                         actuRespStr = binascii.b2a_hex(bytearray(uds_rep), ' ') 
                         cor_RespStr = binascii.b2a_hex(bytearray(resp), ' ') 
                         logging.error(f'correct response:{cor_RespStr}, actual response:[{actuRespStr}]')
                    for i in range(len(resp)):
                         with assume: assert uds_rep[i] == resp[i]
               else:
                    with assume: assert 1 == size
                    logging.error("no response,size = 0")